import json
import codecs
import numpy as np
import glob
import random
import math
import os
from tqdm import tqdm

file_paths = glob.glob('emotion_json_file_6月份/*.json')
day_data_count = []
day_zero = []

# for path in tqdm(file_paths):
#     save_data = {i: [] for i in range(-2, 3)}
#     # save_data = {i: [] for i in range(-1, 2)}
#     # 总数据量
#     total_count = 0
#     # 所有实体情感全为0的数据量
#     zero_data_count = 0
#     # 所有实体情感全为0的数据量
#     zero_data_and_entity_not_union_count = 0
#     # 所有实体情感全为0并且有同义实体的数据量
#     zero_data_and_entity_union_count = 0
#     # 终审数据量
#     final_count = 0
#     # 错误的数据量
#     error_count = 0
#     # 该条数据中存在实体情感不为0的数据量
#     not_only_zero_data_count = 0
#
#     lines = json.load(codecs.open(path, encoding='utf-8'))
#     total_count += len(lines)
#     day_data_count.append(total_count)
#
#     for line in lines:
#         wrong_data = False  # wrong_data==True表示有同义实体或者实体情感中存在不为0的数据
#
#         try:
#             # 计算终审数据量
#             if 'finalData' in line.keys():
#                 #                 print(line.keys())
#                 #                 raise Exception('ting')
#                 final_count += 1
#                 continue
#
#             result_check = line['checkData']['result']
#             #             result_recheck = line['reCheckData']['result']
#
#             # 排除含有iswrong==True的数据
#             for entity, values in result_check.items():
#                 if values['isWrong'] != False:
#                     raise Exception('have error entity')
#
#             # 统计实体情感全为0的数据量
#             all_is_zero = True  # 该段数据的实体情感是不是都是0
#             for entity, values in result_check.items():
#                 emotion = values['emotion']
#
#                 if str(emotion) != '0':  # 该条数据存在实体情感不为0的情况
#                     not_only_zero_data_count += 1
#                     wrong_data = True
#                     all_is_zero = False
#                     break
#
#             # 统计情感全为0的时候有同义实体的数据量
#             if all_is_zero:
#                 zero_data_count += 1
#                 for entity, values in result_check.items():
#                     # 统计有同义实体的数据量
#                     if len(values['code']) != 1:
#                         zero_data_and_entity_union_count += 1
#                         wrong_data = True
#                         break
#
#             if not wrong_data:
#                 zero_data_and_entity_not_union_count += 1
#         except:
#             # raise Exception('ting')
#             error_count += 1
#
#     print('实体情感全为0，并且没有同义实体的数据量为: ', zero_data_and_entity_not_union_count)
#     print('实体情感全为0，并且有同义实体的数据量为: ', zero_data_and_entity_union_count)
#     print('实体情感全为0的数据量为: ', zero_data_count)
#     print('终审数据量为: ', final_count)
#     print('存在实体情感不为0的数据量为: ', not_only_zero_data_count)
#     print('错误的数据量为: ', error_count)
#     print('总的数据量为: ', total_count)
#
#     no_method = 2 * total_count + final_count
#     method = 2 * total_count - zero_data_and_entity_not_union_count
#     day_zero.append((no_method - method) / no_method)
#     print('节省人力:{}'.format((no_method - method) / no_method))
#     print('*' * 20)

total = 0
total_zero = 0
for path in tqdm(file_paths):
    save_data = {i: 0 for i in range(-2, 3)}
    # save_data = {i: [] for i in range(-1, 2)}
    # 总数据量
    total_count = 0

    # 标签为0，一标二标同意实体相同
    zero_have_same_xt = 0
    # 标签为0，一标二标同意实体不同
    zero_have_same_bt = 0
    # 标签为0，有同意实体的数量
    zero_have_same = 0

    lines = json.load(codecs.open(path, encoding='utf-8'))
    total_count += len(lines)
    cur_day_count = 0

    for line in lines:
        try:
            result_check = line['checkData']['result']
            result_recheck = line['reCheckData']['result']

            for entity, values in result_check.items():
                cur_day_count += 1
                if values['isWrong'] != False or result_recheck[entity]['isWrong'] != False:
                    continue

                emotion = values['emotion']

                if emotion != result_recheck[entity]['emotion']:  # 交叉不一致就跳过
                    continue

                save_data[emotion] += 1
                total += 1
                if emotion == 0:
                    total_zero += 1

                if (emotion == 0) and (len(values['code']) > 1):
                    zero_have_same += 1
                    check_code = values['code']
                    check_code.sort()
                    recheck_code = result_recheck[entity]['code']
                    recheck_code.sort()
                    if check_code == recheck_code:
                        zero_have_same_xt += 1
                    else:
                        zero_have_same_bt += 1

        except Exception as e:
            #             print(e)
            continue

    print(f'当天-2: {save_data[-2]}条, -1: {save_data[-1]}条, 0: {save_data[0]}条, 1: {save_data[1]}条, 2: { save_data[2]}条')
    print(f'数据总量: {sum(save_data.values())}')
    print(f'0数量是其他数量的{save_data[0] / (save_data[-2]+save_data[-1]+save_data[1]+save_data[2])}倍')
    print(f'标签为0，一标二标同意实体相同: {zero_have_same_xt}')
    print(f'标签为0，一标二标同意实体不同: {zero_have_same_bt}')
    print(f'标签为0，有同意实体的数量: {zero_have_same}')
    print(f'同意实体标注相同占的比率为{zero_have_same_xt/zero_have_same}')
    print(f'标签为0，有同意实体的量占比{zero_have_same/save_data[0]}')
    print('*' * 20)

print(f"数据总量:{total}")
print(f"0总量:{total_zero}")

